﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MakeIT_Bill_Maker
{
    public partial class frmCharges : Form
    {
        private Database.DatabaseConnection conn = new Database.DatabaseConnection();
        private DataSet dataset;
        public frmCharges()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            timer1.Start();
            dispaly_Data();
        }
        
        private void txtEnd_Validating(object sender, CancelEventArgs e)
        {
            int out_;
            string text = ((TextBox)sender).Text;
            if (!int.TryParse(text, out out_))
            {
                MessageBox.Show("End Range must be a number.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                //e.Cancel = true;
            }
            else
            {
                if (out_ <= int.Parse(txtStart.Text))
                {
                    MessageBox.Show("End Range must be greater than Start Range.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                   // e.Cancel = true;
                }
            }
        }

        //Clear
        private void cmdClear_Click(object sender, EventArgs e)
        {
            clear_All();

        }

        private void clear_All()
        {
            txtStart.Text = "";
            txtEnd.Text = "";
            txtCost.Text = "";
            txtFixedCharge.Text = "";
            txtStart.Focus();
        }

        //Add
        private void btnAdd_Click(object sender, EventArgs e)
        {
            int out_, out_2;
            double out_3;
            string start = txtStart.Text;
            string end = txtEnd.Text;
            string cost = txtCost.Text;
            string fixedCharge = txtFixedCharge.Text;
            string range;
            string sql;
            if (!int.TryParse(start, out out_))
            {
                MessageBox.Show("Start Range must be a number.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else if (!int.TryParse(end, out out_2))
            {
                MessageBox.Show("End Range must be a number.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else if (out_2 <= out_)
            {
                MessageBox.Show("End Range must be greater than Start Range.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else if (!double.TryParse(cost, out out_3))
            {
                MessageBox.Show("Unit Charge is a number.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else if (!double.TryParse(fixedCharge, out out_3))
            {
                MessageBox.Show("Fixed Charge is a number.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                range = out_ + "-" + out_2;
                sql = "INSERT INTO Charges (Units, Charge, FixedCharge) VALUES ('" + out_ + "-" + out_2 + "','" + cost + "','" + fixedCharge + "')";
                bool b = conn.add(sql);
                

                if (b)
                {
                    MessageBox.Show("Record is added succesfully.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    dispaly_Data();
                    clear_All();
                }
                else
                {
                    MessageBox.Show("Failed to add new Record.", "Failure", MessageBoxButtons.OK, MessageBoxIcon.Error);

                }
            }
        }

        //Timer
        private void timer1_Tick(object sender, EventArgs e)
        {

            if (!(txtStart.Text.Trim().Equals("")) && !(txtEnd.Text.Trim().Equals("")) && !(txtCost.Text.Trim().Equals("")) && !(txtFixedCharge.Text.Trim().Equals("")))
            {
               btnAdd.Enabled = true;
            }
            else
            {
                btnAdd.Enabled = false;
            }

            if (dataGridView1.SelectedRows.Count > 0)
            {
                btnDelete.Enabled = true;
            }
            else
            {
                btnDelete.Enabled = false;
            }
        }

        //refresh data
        private void dispaly_Data()
        {
            string sql = "SELECT * FROM Charges";
            dataset = conn.Show(sql);
            dataGridView1.DataSource = dataset.Tables[0].DefaultView;
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            DataSet changedRecords = dataset.GetChanges();
            if (changedRecords != null)
            {
                DataTable dt = changedRecords.Tables[0];
                foreach (DataRow row in dt.Rows)
                {
                    string key = row[0].ToString();
                    string val = row[1].ToString();
                    string fixedCharge = row[2].ToString();
                    string sql = "UPDATE Charges SET Charge = '" + val + "', FixedCharge =  '" + fixedCharge + "' WHERE Units = '" + key + "'";
                    bool b = conn.edit(sql);
                }
            }          
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            string sql;
            for (int i = 0; i < dataGridView1.SelectedRows.Count; i++)
            {
                string index = dataGridView1.SelectedRows[i].Cells[0].Value.ToString();
                sql = "DELETE FROM Charges WHERE Units = '" + index + "'";
                bool b = conn.delete(sql);
                if (b)
                {

                    MessageBox.Show("Record(s) deleted succesfully.");
                }
                else
                {
                    MessageBox.Show("Record(s) deleting failed.");
                }
            }
            dispaly_Data();
        }
    }
}
